<template>
  <div class="app-container">
    <div class="filter-tools">
      <el-form :inline="true" :model="filter" class="filter-form">
        <el-form-item label="状态">
          <el-select
            v-model="filter.status"
            placeholder="请选择状态"
            clearable
          >
            <el-option v-for="(data,i) in status" :key="i" :label="data" :value="i"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input
            v-model="filter.title"
            placeholder="标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input
            v-model="filter.note"
            placeholder="内容"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="标签">
          <el-input
            v-model="filter.tags"
            placeholder="标签"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="ID">
          <el-input
            v-model="filter.id"
            placeholder="ID"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="filter.start_at"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="~至~">
          <el-date-picker
            v-model="filter.end_at"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="23:59:59"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList()">
            查找
          </el-button>
        </el-form-item>
        <el-form-item style="float:right">
          <el-button type="success" icon="el-icon-plus" @click="handleAdd()">
            记事
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table">
      <el-table v-loading="dataLoading" :data="dataList" :height="height" border>
        <el-table-column
          prop="id"
          label="ID"
          align="center"
          width="80"
        />
        <el-table-column
          prop="title"
          label="标题"
          align="center"
        />
        <el-table-column label="内容" min-width="200">
          <template slot-scope="scope">
            <div @click="articleNote(scope.row.note)" type="primary" style="margin-left: 16px;" v-html="scope.row.note ? scope.row.note.substring(0,20) + '->点击看全部':''"></div>
          </template>
        </el-table-column>
        <el-table-column label="标签" min-width="150">
          <template slot-scope="scope">
            <el-tag
              style="margin-bottom:5px"
              v-for="item in scope.row.tags"
              :key="item"
              :type="item"
              effect="dark">
              {{ item }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="处理状态" align="center">
          <template slot-scope="scope">
            <div><el-tag :type="scope.row.status === 2 ? 'success' : 'info'">{{status[scope.row.status]}}</el-tag></div>
          </template>
        </el-table-column>
        <el-table-column label="时间" width="220">
          <template slot-scope="scope">
            <div><b>更新时间:</b>{{scope.row.updated_at}}</div>
            <div><b>创建时间:</b>{{scope.row.created_at}}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button-group>
              <el-button
                icon="el-icon-edit"
                type="primary"
                size="mini"
                @click="handleEdit(scope.row)"
              >
                编辑
              </el-button>
              <el-button
                v-if="scope.row.status == 1"
                icon="el-icon-folder-checked"
                type="success"
                size="mini"
                @click="setCate(scope.row, 2)"
              >
                标记为处理完成
              </el-button>
              <el-button
                v-if="scope.row.status == 2"
                icon="el-icon-folder-delete"
                type="danger"
                size="mini"
                @click="setCate(scope.row, 1)"
              >
                标记为未完成处理
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="filter.page"
        :page-sizes="[10, 20, 30, 50, 100]"
        :page-size="filter.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="filter.total"
        @size-change="handleSizeChange"
        @current-change="getList"
      ></el-pagination>
    </div>

    <el-drawer
      :visible.sync="drawer_article_note"
      :with-header="false">
      <div style="padding: 35px" v-html="article"></div>
    </el-drawer>

    <el-dialog title="编辑" :visible.sync="dialogStatus" width="900px">
      <el-form :model="formData" label-width="80px">
        <el-form-item label="标题" >
          <el-input v-model="formData.title" placeholder="" class="filter-item"/>
        </el-form-item>
        <el-form-item class="vab-quill-content" label="内容">
          <vab-quill
            ref="article"
            v-model="formData.note"
            :min-height="800"
            :options="options"
          />
        </el-form-item>
        <el-form-item label="标签" >
          <el-tag
            :key="tag"
            v-for="tag in formData.tags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{tag}}
          </el-tag>
          <el-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          >
          </el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+新标签</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogStatus = false">取消</el-button>
        <el-button type="primary" @click="saveForm()">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from '@/api/sundry/notepadApi' // API接口
import VabQuill from '@/extra/VabQuill'
import VabUpload from '@/extra/VabUpload'
export default {
  name: 'notepad',
  components: { VabQuill, VabUpload },
  data() {
    return {
      height:this.$baseTableHeight(1),
      status:{
        null:'全部',
        1:'待处理',
        2:'已处理'
      },
      filter: {
        id:"",
        page: 1,
        pageSize: 10,
        total: 0,
        tags:"",
        title: '',
        start_at:'',
        end_at:"",
        note:"",
        status: "null",
      },
      options: {
        theme: 'snow',
        bounds: document.body,
        debug: 'warn',
        modules: {
          toolbar: {
            container: [
              ['bold', 'italic', 'underline', 'strike'],
              [{ header: [1, 2, 3, 4, 5, 6, false] }],
              [{ size: ['small', false, 'large', 'huge'] }],
              [{ color: [] }, { background: [] }],
              ['blockquote', 'code-block'],
              [{ list: 'ordered' }, { list: 'bullet' }],
              [{ script: 'sub' }, { script: 'super' }],
              [{ indent: '-1' }, { indent: '+1' }],
              [{ align: [] }],
              [{ direction: 'rtl' }],
              [{ font: [] }],
              ['clean'],
              ['link'],
              // , 'image', 'vab-upload-image'
            ],
          },
        },
        placeholder: '内容...',
        readOnly: false,
      },
      dataLoading: true,
      dataList: [],
      dialogStatus:false,
      formData:{},
      topStatus:0,
      dynamicTags: ['标签一', '标签二', '标签三'],
      inputVisible: false,
      inputValue: '',
      article:"",
      drawer_article_note:false
    }
  },
  created() {
    this.filter.id = this.$route.query.module_id || null
    this.getList()
  },
  methods: {
    getList(page = 1) {
      this.filter.page = Number(page)
      api.list(this.filter).then((res) => {
        this.dataList = res.data.list
        this.filter.total = res.data.total
        this.dataLoading = false
      })
    },
    articleNote(data) {
      this.article = data
      this.drawer_article_note = true
    },
    handleSizeChange(val) {
      this.filter.pageSize = val
      this.getList(1)
    },
    handleAdd(){
      this.formData = {
        note:"",
        title:'',
        tags:[],
      },
        this.dialogStatus = true
    },
    handleEdit(data){
      this.formData = {
        id:data.id,
        tags: data.tags,
        title: data.title,
        note: data.note,
      },
        this.dialogStatus = true
    },
    saveForm(){
      api.save(this.formData).then((res) => {
        this.getList();
        this.$message.success(res.userMsg)
        this.dialogStatus = false
      })
    },
    handleClose(tag) {
      this.formData.tags.splice(this.formData.tags.indexOf(tag), 1);
    },
    setCate(data,enable){
      api.set({id:data.id,status:enable}).then((res) => {
        this.getList();
        this.$message.success(res.userMsg)
      })
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.formData.tags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = '';
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
  },

}
</script>

<style>
.avatar-uploader .el-upload{
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
</style>
<style lang="scss" scoped>
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
.goto:hover{color: #0051ff;}
.goto{
  cursor:pointer
}
</style>



